package com.jh.fcsm.util.security;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class Base64Converter {
    public static final Logger logger = LoggerFactory.getLogger(Base64Converter.class);
    private final static Base64.Encoder encoder = Base64.getEncoder();
    private final static Base64.Decoder decoder = Base64.getDecoder();

    /**
     * 给字符串加密
     *
     * @param text
     * @return
     */
    public static String encode(String text) {
        byte[] textByte = text.getBytes(StandardCharsets.UTF_8);
        String encodedText = encoder.encodeToString(textByte);
        return encodedText;
    }

    /**
     * 将加密后的字符串进行解密
     *
     * @param encodedText
     * @return
     */
    public static String decode(String encodedText) {
        String text = null;
        text = new String(decoder.decode(encodedText), StandardCharsets.UTF_8);
        return text;
    }

    public static void main(String[] args) throws UnsupportedEncodingException {

        String username = "zhishichanquan/1dfe7589dd807189612cc35f32bc0474.docx";
        String p = "p@sSW0rd";

        // 加密
        System.out.println("====  [加密后] 用户名/密码  =====");
        System.out.println(Base64Converter.encode(username));
        System.out.println(Base64Converter.encode(p));

        // 解密
        System.out.println("\n====  [解密后] 用户名/密码  =====");
        System.out.println(Base64Converter.decode(Base64Converter.encode(username)));
        System.out.println(Base64Converter.decode(Base64Converter.encode(p)));
    }
}
